#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

pthread_t maintid;

void* pthreadrun(void* args)
{
  pthread_detach(pthread_self());
  int num = (int)args;
  
     printf("i'm child[0x%x], i want to say %d\n", pthread_self(), num);
     //pthread_cancel(maintid);
 // while(1){
    // sleep(1);
     //pthread_exit((void*)num);
     //if(num == 3)
     //{
     //  int* p = NULL;
     //  *p = 1000;
     //}
  //}
  return (void*)num;
}

void* newthread(void* args)
{
     pthread_detach(pthread_self());
    int num = (int)args;
     printf("i'm child[0x%x], i want to say %d\n", pthread_self(), num);
     sleep(2);
     return (void*)123;
}

#define NUM 5

int main()
{
  pthread_t tid[NUM + 1];
  maintid = pthread_self();

  for(int i = 0; i < NUM; ++i)
  {
    pthread_create(tid + i, NULL, pthreadrun, (void*)i);
    sleep(1);
  }
  //pthread_create(tid + NUM, NULL, pthreadrun, (void*)5);
  printf("wait for waiting\n");
  sleep(1);

  void* status = NULL;
 // for(int i = 0; i < NUM; ++i)
 // {
 //   //pthread_cancel(tid[i]);
 //   printf("%d号线程已取消\n", i);
 //   pthread_join(tid[i], &status);
 //   printf("%d号线程已被回收, 回收状态为%d\n", i,(int)status);
 // }

  while(1);
  return 0;
}
